package com.link.base.base.project.controller;

import com.link.base.base.project.model.AcceptanceList;
import com.link.base.base.project.service.AcceptanceListService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import com.link.core.cllog.LCLogger;
import com.link.core.modules.export.ExportFactory;
import com.link.core.modules.export.ExportWriter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目承接单
 *
 * @author 王昊
 * @date 2020-10-13 14:01:06
 */
@Controller
@RequestMapping("/link/acceptanceList")
public class AcceptanceListController extends BasicController<AcceptanceList> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private AcceptanceListService acceptanceListService;

    @Override
    public BasicService<AcceptanceList> getBasicService() throws Exception {
        return acceptanceListService;
    }

    public void beforUpsert(AcceptanceList entity, HttpServletRequest request) throws Exception {
        if (null == entity || null == entity.getProjectGrossPer()) {
            throw new RuntimeException("项目毛利不能为空!");
        }

    }

    /**
     * 承接单提交
     *
     * @author 王昊
     * @date 2020/11/4
     */
    @RequestMapping(value = "/acceptanceListSubmit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> acceptanceListSubmit(@JsonParam AcceptanceList entity, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            acceptanceListService.acceptanceListSubmit(entity);
            result.put("newRow",entity);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     *年度新项目承接管理表
     *
     * @author 王昊
     * @date 2020/11/4
     */
    @RequestMapping(value = "/projectUndertakingReport", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> projectUndertakingReport(@JsonParam QueryParams queryParams, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            AcceptanceList entity = BasicModel.transformClass(AcceptanceList.class,queryParams);
            List<AcceptanceList> list = acceptanceListService.projectUndertakingReport(entity);
            result.put("rows",list);
            result.put("success", true);
            result.put("total", getCount(entity, list.size()));
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     *年度新项目承接管理报表 导出报表
     * @param qps
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/projectUndertakingReport/exportData")
    @ResponseBody
    public Map<String, Object> projectUndertakingReport(@JsonParam QueryParams qps, HttpSession session,
                                                        HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<AcceptanceList> list = new ArrayList<>();
        try {
            ExportWriter exportWriter;
            String exportType = "xlsx";
            String exportFileType = qps.getExportFileType();
            if (exportType.equals(exportFileType)) {
                exportWriter = ExportFactory.newExcelInstance(qps);
            } else {
                exportWriter = ExportFactory.newInstance(qps);
            }
            AcceptanceList entity = BasicModel.transformClass(AcceptanceList.class,qps);
            while (!qps.getStopExport()) {
                list = acceptanceListService.projectUndertakingReport(entity);
                exportWriter.wirteData(list);
                if ("Y".equals(qps.getExportCurrentPage())) {
                    break;
                }
            }

            result.put("success", true);
            result.put("url", exportWriter.getUrlPath());
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 项目承接实绩零件类型销售额报表（柱状图）
     * @param entity
     * @param session
     * @param request
     * @return
     */
    @RequestMapping(value = "/underTakePartReport",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> underTakePartReport(@JsonParam AcceptanceList entity, HttpSession session, HttpServletRequest request) {
        Map<String,Object> result = new HashMap<>(4);
        try {
            List acceptanceLists = acceptanceListService.queryAllByDate(entity);
            result.put("success",true);
            //将cha返回
            result.put("newRow",acceptanceLists);
        } catch (Exception e) {
            result.put("success",false);
            result.put("result",e.getMessage());
        }
        return result;
    }



    /**
     * 项目承接实绩主机厂销售额报表（柱状图）
     * @param entity
     * @param session
     * @param request
     * @return
     */
    @RequestMapping(value = "/underTakeEngineeReport",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> underTakeEngineeReport(@JsonParam AcceptanceList entity, HttpSession session, HttpServletRequest request) {
        Map<String,Object> result = new HashMap<>(4);
        try {
            List acceptanceLists = acceptanceListService.queryAllByDateEnginee(entity);
            result.put("success",true);
            //将cha返回
            result.put("newRow",acceptanceLists);
        } catch (Exception e) {
            result.put("success",false);
            result.put("result",e.getMessage());
        }
        return result;
    }


}
